package algorithm.dynamic_planning.leetcode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 第一个动态规划问题，青蛙跳台阶&爬楼梯
 * https://leetcode-cn.com/problems/climbing-stairs/
 */
public class Num70 {
    // 解法1.递归求解
    public int climbStairs(int n) {
        if (n == 0 || n == 1)
            return 1;
        return climbStairs(n - 1) + climbStairs(n - 2);
    }

    // 解法2.记忆化搜索
    int[] num;
    public int climbStairs1(int n) {
        num = new int[n + 1];
        Arrays.fill(num,-1);
        return climbInner(n);
    }

    private int climbInner(int n) {
        if (n == 0 || n == 1)
            return 1;
        if (num[n] == -1) {
            int value = climbInner(n - 1) + climbInner(n - 2);
            num[n] = value;
        }
        return num[n];
    }

    // 解法3.动态规划
    public int climbStairs3(int n) {
        int[] memo = new int[n + 1];
        memo[0] = 1;
        memo[1] = 1;
        for (int i = 2; i <= n; i++) {
            memo[i] = memo[i - 1] + memo[i - 2];
        }
        return memo[n];
    }
}
